﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.ProductInfo;

namespace com.subb.ProductInfo
{
    public class PP_RecomProductDAL
    {
        public List<PP_RecommendProduct> GetAllRecomProduct(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            SugardbEntities ctx = new SugardbEntities();
            var query = ctx.PP_RecommendProduct.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.RecomID);
            ttlCount = query.Count();
            List<PP_RecommendProduct> prodlist = query.Skip(start).Take(limit).ToList();
            return prodlist;
        }

        public List<PP_RecommendProduct> GetAllRecomProduct(int start, int limit, out int ttlCount, string lang)
        {
            ttlCount = 0;
            SugardbEntities ctx = new SugardbEntities();
            var query = ctx.PP_RecommendProduct.Where(c => c.PP_Product.PP_ProductCata != null && c.PP_Product.PP_ProductCata.Lang == lang).OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.RecomID);
            ttlCount = query.Count();
            List<PP_RecommendProduct> prodlist = query.Skip(start).Take(limit).ToList();
            return prodlist;
        }

        public PP_RecommendProduct GetRecomProduct(int recomId)
        {
            SugardbEntities ctx = new SugardbEntities();
            PP_RecommendProduct rprod = ctx.PP_RecommendProduct.FirstOrDefault(c => c.RecomID == recomId);
            return rprod;
        }

        public PP_RecommendProduct GetRecomProductByProdId(int prodId)
        {
            SugardbEntities ctx = new SugardbEntities();
            PP_RecommendProduct rprod = ctx.PP_RecommendProduct.FirstOrDefault(c => c.ProductID == prodId);
            return rprod;
        }

        public int CreateRecomProduct(RecomProduct rprod)
        {
            int recomId = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                PP_RecommendProduct wirprod = new PP_RecommendProduct
                {
                    RecomID = rprod.RecomID,
                    ProductID = rprod.ProductID,
                    IndexCover = rprod.IndexCover,
                    SortNum = rprod.SortNum,
                    IndexArea = rprod.IndexArea
                };
                ctx.PP_RecommendProduct.AddObject(wirprod);
                ctx.SaveChanges();
                recomId = wirprod.RecomID;
            }
            return recomId;
        }

        public bool UpdateRecomProduct(int recomId, RecomProduct rprod)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                PP_RecommendProduct wirprod = ctx.PP_RecommendProduct.FirstOrDefault(c => c.RecomID == recomId);
                if (wirprod != null)
                {
                    wirprod.IndexCover = rprod.IndexCover;
                    wirprod.SortNum = rprod.SortNum;
                    wirprod.IndexArea = rprod.IndexArea;
                    ctx.SaveChanges();
                }
            }
            return result;
        }

        public bool RemoveRecomProduct(int recomId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                PP_RecommendProduct wirprod = ctx.PP_RecommendProduct.FirstOrDefault(c => c.RecomID == recomId);
                if (wirprod != null)
                {
                    ctx.PP_RecommendProduct.DeleteObject(wirprod);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }
    }
}
